package com.zhentao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhentao.mapper.SysUserRoleMapper;
import com.zhentao.model.dto.system.SysRoleDto;
import com.zhentao.model.entity.system.SysRole;
import com.zhentao.service.SysRoleService;
import com.zhentao.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 懒阳阳
* @description 针对表【sys_role(角色)】的数据库操作Service实现
* @createDate 2025-02-08 14:45:04
*/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService{

    @Autowired
    private SysRoleMapper sysRoleMapper ;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper ;

    @Override
    public PageInfo<SysRole> findByPage(SysRoleDto sysRoleDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysRole> sysRoleList = sysRoleMapper.findByPage(sysRoleDto);
        PageInfo<SysRole> pageInfo = new PageInfo<>(sysRoleList);
        return pageInfo;
    }

    @Override
    public void saveSysRole(SysRole sysRole) {
        sysRoleMapper.insert(sysRole);
    }

    @Override
    public void updateSysRole(SysRole sysRole) {
        sysRoleMapper.updateRoleById(sysRole);
    }

    @Override
    public void deleteById(Long roleId) {
        sysRoleMapper.deleteRoleById(roleId);
    }

    @Override
    public Map<String, Object> findAllRoles(Long userId) {
        //查询所有角色的数据
        List<SysRole> sysRoleList = sysRoleMapper.findAllRoles();

        //查询当前登录用户的角色数据
        List<String> sysRoles = sysUserRoleMapper.findSysUserRoleByUserId(userId);

        //构建响应结果数据
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("allRolesList",sysRoleList);
        resultMap.put("sysUserRoles",sysRoles);

        //返回结果
        return resultMap;
    }
}




